/*
 * matrixU.cpp
 *
 *  Created on: Oct 28, 2012
 *      Author: monitz
 */

#include <fstream>
using namespace std;
#include <iostream>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include "matrixU.h"
//look for free space in course, enroll student and return the weekday integer
int enroll(vector<vector<string> > *courseVec, string courseName,string studName) {
	//iterate through the courses vector and look for matching Course-ID with free space
	for (unsigned int i = 0; i < courseVec->size(); i++) {
		if (courseVec->at(i).at(1).compare(courseName) == 0) {
			//check if course has room
			if (courseVec->at(i).at(courseVec->at(i).size() - 1).compare("")== 0) {
				//enroll student to class
				for (unsigned int j = 2; j < courseVec->at(i).size(); j++) {
					if (courseVec->at(i).at(j).compare("") == 0) {
						courseVec->at(i).at(j) = studName;
						j = courseVec->at(i).size();
					}
				}
				//enrollment was succesful, return weekday of course
				return atoi(courseVec->at(i).at(0).c_str()) - 1;
			}
		}
	}
	//enrollment failed, no free space or no such course, return -1
	return -1;
}
//parse the student file and enroll the students, returns a vector of vectors containing the list of courses for each day
vector<vector<string> >* parseStudents(vector<vector<string> > *courseVec) {
	vector<vector<string> > *ans = new vector<vector<string> > ;
	string line, Cname, Sname;
	int wd = -1;
	int loc = 0;
	stringstream sstm;
	vector<string> templine;
	ifstream fh("students.conf");
	if (fh.is_open()) {
		while (fh.good()) {
			getline(fh, line);
			//make room for seven days of the week
			templine.resize(7);
			loc = 0;
			//read the student name
			Sname = line.substr(0, line.find_first_of(','));
			templine.at(0) = Sname;
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//start parsing the courses from the line
			while (loc != -1) {
				loc = line.find_first_of(',');
				Cname = line.substr(0, loc);
				//try to enroll student to desired course
				wd = enroll(courseVec, Cname, Sname);
				if (wd >= 0) {
					//enrollment successful, store in vector meant for weekday
					sstm.str("");
					sstm << "\n\t" << wd + 1 << "\t" << Cname;
					templine.at(wd).append(sstm.str());
				}
				if (loc != -1) {
					//remove parsed part from line
					line = line.substr(loc + 1, line.length());
				} else {
					//all done parsing line
					line.clear();
				}
			}
			ans->push_back(templine);
			templine.clear();
		}
		fh.close();
	} else
		cout << "Unable to open file";
	return ans;

}
//parse the courses file, returning a vector of string vectors for each course containing empty cells for students to enroll
vector<vector<string> >* parseCourses() {
	vector<vector<string> > *ans = new vector<vector<string> > ;
	string line;
	int size;
	vector<string> templine;
	ifstream fh("courses.conf");
	if (fh.is_open()) {
		while (fh.good()) {
			getline(fh, line);
			//parse weekday
			templine.push_back(line.substr(0, line.find_first_of(',')));
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//parse course name
			templine.push_back(line.substr(0, line.find_first_of(',')));
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//parse space from string to be used for vector size
			size = atoi(line.substr(0, line.find_first_of(',')).c_str());
			line = line.substr(0, line.find_first_of(',') + 1);
			//resize vector to fit students +2 for name and weekday
			templine.resize(2 + size);
			ans->push_back(templine);
			templine.clear();

		}
		fh.close();
	}

	else
		cout << "Unable to open file";

	return ans;
}

int main() {

	//temp_student->at(0)= "monitz";
	vector<vector<string> > *courseVec = parseCourses();
	vector<vector<string> > *studVec = parseStudents(courseVec);
	ofstream fhs("students.out");
	ofstream fhc("courses.out");
	//go through the courses, print the enrolled students into file
	for (unsigned int j = 0; j < courseVec->size(); j++) {
		//print course header
		fhc << courseVec->at(j).at(0) << " " << courseVec->at(j).at(1) << endl;
		for (unsigned int i = 2; i < courseVec->at(j).size(); i++)
			//print non-empty enrolled students
			if (courseVec->at(j).at(i).compare("") == 0)
				i = courseVec->at(j).size();
			else
				fhc << '\t' << courseVec->at(j).at(i) << endl;
	}
	//go through the students, print the courses the student is enrolled to into file
	for (unsigned int j = 0; j < studVec->size(); j++) {
		//go through the seven weekday, print courses
		for (unsigned int i = 0; i < studVec->at(j).size(); i++) {
			fhs << studVec->at(j).at(i);
		}
		fhs << endl;
	}
	delete courseVec;
	delete studVec;
	fhs.close();
	fhc.close();
}
